package org.example.sink;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.connector.file.sink.FileSink;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.OutputFileConfig;
import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.DateTimeBucketAssigner;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.time.ZoneId;

import static org.apache.flink.connector.file.sink.FileSink.forRowFormat;

public class SinkDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.enableCheckpointing(5000);
                DataGeneratorSource<String> dataGeneratorSource =
                new DataGeneratorSource<>(
                        new GeneratorFunction<Long, String>() {
                            @Override
                            public String map(Long value) throws Exception {
                                return "Number:"+value;
                            }
                        },
                        Long.MAX_VALUE,
                        RateLimiterStrategy.perSecond(1000),
                        Types.STRING
                );
        DataStreamSource<String> source = env.fromSource(dataGeneratorSource, WatermarkStrategy.noWatermarks(), "genSource");


        FileSink fileSink =

                forRowFormat(new Path("H:\\tmp"), new SimpleStringEncoder<String>("UTF-8"))
                .withOutputFileConfig(OutputFileConfig
                        .builder()
                        .withPartPrefix("atguigu-")
                        .withPartSuffix(".log")
                        .build()
                )
                .withBucketAssigner(
                        new DateTimeBucketAssigner("yyyy-MM-dd HH", ZoneId.systemDefault())
                ).withRollingPolicy(
                        DefaultRollingPolicy.builder()
                                .withRolloverInterval(10000)
                                .withMaxPartSize(1024*1024)
                                .build()
                ).build();
        source.sinkTo(fileSink);
        env.execute();
    }
}
